package com.sinsz.wxpn.open.message.impl;

import com.sinsz.wxpn.open.BaseWxpn;
import com.sinsz.wxpn.open.message.ReplyService;
import com.sinsz.wxpn.properties.WxpnProperties;
import org.apache.commons.lang3.StringUtils;

/**
 * 回复消息实现
 * @author chenjianbo
 * @date 2018-11-16
 */
public class ReplyServiceImpl extends BaseWxpn implements ReplyService {

    public ReplyServiceImpl(WxpnProperties prop) {
        super(prop);
    }

    @Override
    public String replyTextMessage(String openID, String content) {
        if (StringUtils.isEmpty(openID)) {
            return "";
        }
        content = StringUtils.isEmpty(content) ? "" : content;
        String message = "<xml>" +
                "<ToUserName><![CDATA[%1$s]]></ToUserName>" +
                "<FromUserName><![CDATA[%2$s]]></FromUserName>" +
                "<CreateTime>%3$d</CreateTime>" +
                "<MsgType><![CDATA[text]]></MsgType>" +
                "<Content><![CDATA[%4$s]]></Content>" +
                "</xml>";
        return String.format(message, openID, super.prop().getWechatId(), System.currentTimeMillis() / 1000, content);
    }

    @Override
    public String replyImageMessage(String openID, String mediaId) {
        if (StringUtils.isEmpty(openID) || StringUtils.isEmpty(mediaId)) {
            return "";
        }
        String message = "<xml>" +
                "<ToUserName><![CDATA[%1$s]]></ToUserName>" +
                "<FromUserName><![CDATA[%2$s]]></FromUserName>" +
                "<CreateTime>%3$d</CreateTime>" +
                "<MsgType><![CDATA[image]]></MsgType>" +
                "<Image><MediaId><![CDATA[%4$s]]></MediaId></Image>" +
                "</xml>";
        return String.format(message, openID, super.prop().getWechatId(), System.currentTimeMillis() / 1000, mediaId);
    }

    @Override
    public String replyVoiceMessage(String openID, String mediaId) {
        if (StringUtils.isEmpty(openID) || StringUtils.isEmpty(mediaId)) {
            return "";
        }
        String message = "<xml>" +
                "<ToUserName><![CDATA[%1$s]]></ToUserName>" +
                "<FromUserName><![CDATA[%2$s]]></FromUserName>" +
                "<CreateTime>%3$d</CreateTime>" +
                "<MsgType><![CDATA[voice]]></MsgType>" +
                "<Voice><MediaId><![CDATA[%4$s]]></MediaId></Voice>" +
                "</xml>";
        return String.format(message, openID, super.prop().getWechatId(), System.currentTimeMillis() / 1000, mediaId);
    }

    @Override
    public String replyVideoMessage(String openID, String mediaId, String title, String description) {
        if (StringUtils.isEmpty(openID) || StringUtils.isEmpty(mediaId)) {
            return "";
        }
        title = StringUtils.isEmpty(title) ? "" : title;
        description = StringUtils.isEmpty(description) ? "" : description;
        String message = "<xml>" +
                "<ToUserName><![CDATA[%1$s]]></ToUserName>" +
                "<FromUserName><![CDATA[%2$s]]></FromUserName>" +
                "<CreateTime>%3$d</CreateTime>" +
                "<MsgType><![CDATA[video]]></MsgType>" +
                "<Video>" +
                "<MediaId><![CDATA[%4$s]]></MediaId>" +
                ( "".equals(title) ? "%5$s" : "<Title><![CDATA[%5$s]]></Title>") +
                ( "".equals(description) ? "%6$s" : "<Description><![CDATA[%6$s]]></Description>") +
                "</Video>" +
                "</xml>";
        return String.format(message, openID, super.prop().getWechatId(), System.currentTimeMillis() / 1000, mediaId, title, description);
    }

    @Override
    public String replyMusicMessage(String openID, String title, String description, String musicUrl, String musicHdUrl, String thumbMediaId) {
        if (StringUtils.isEmpty(openID) || StringUtils.isEmpty(thumbMediaId) || StringUtils.isEmpty(musicUrl)) {
            return "";
        }
        title = StringUtils.isEmpty(title) ? "音乐" : title;
        description = StringUtils.isEmpty(description) ? "美美的来一首!" : description;
        musicHdUrl = StringUtils.isEmpty(musicHdUrl) ? musicUrl : musicHdUrl;
        String message = "<xml>" +
                "<ToUserName><![CDATA[%1$s]]></ToUserName>" +
                "<FromUserName><![CDATA[%2$s]]></FromUserName>" +
                "<CreateTime>%3$d</CreateTime>" +
                "<MsgType><![CDATA[music]]></MsgType>" +
                "<Music>" +
                ( "".equals(title) ? "%4$s" : "<Title><![CDATA[%4$s]]></Title>") +
                ( "".equals(description) ? "%5$s" : "<Description><![CDATA[%5$s]]></Description>") +
                "<MusicUrl><![CDATA[%6$s]]></MusicUrl>" +
                ( "".equals(musicHdUrl) ? "%7$s" : "<HQMusicUrl><![CDATA[%7$s]]></HQMusicUrl>") +
                "<ThumbMediaId><![CDATA[%8$s]]></ThumbMediaId>" +
                "</Music>" +
                "</xml>";
        return String.format(message, openID, super.prop().getWechatId(), System.currentTimeMillis() / 1000, title, description, musicUrl, musicHdUrl, thumbMediaId);
    }

    @Override
    public String replyNewsMessage(String openID, String title, String description, String picUrl, String url) {
        if (StringUtils.isEmpty(openID)
                || StringUtils.isEmpty(title)
                || StringUtils.isEmpty(description)
                || StringUtils.isEmpty(picUrl)
                || StringUtils.isEmpty(url)) {
            return "";
        }
        String message = "<xml>" +
                "<ToUserName><![CDATA[%1$s]]></ToUserName>" +
                "<FromUserName><![CDATA[%2$s]]></FromUserName>" +
                "<CreateTime>%3$d</CreateTime>" +
                "<MsgType><![CDATA[news]]></MsgType>" +
                "<ArticleCount><![CDATA[1]]></ArticleCount>" +
                "<Articles>" +
                "<item>" +
                "<Title><![CDATA[%4$s]]></Title>" +
                "<Description><![CDATA[%5$s]]></Description>" +
                "<PicUrl><![CDATA[%6$s]]></PicUrl>" +
                "<Url><![CDATA[%7$s]]></Url>" +
                "</item>" +
                "</Articles>" +
                "</xml>";
        return String.format(message, openID, super.prop().getWechatId(), System.currentTimeMillis() / 1000, title, description, picUrl, url);
    }

}
